﻿//**************************************************
//CLASS : tcdc_mst_campaign
//AUTHOR : PATTARAWUT CHANNGAM & SUCHIN INPIRUT
//LAST MODIFIED : Monday, February 25, 2013
//**************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Model
{

public class tcdc_mst_campaign
{

     private DBUtil DB;
     public string strConn { get; set; }
     public int campaign_id {get;set;}
     public int product_id {get;set;}
     public int member_type_id {get;set;}
     public int campaign_group_id {get;set;}
     public string campaign_name_th {get;set;}
     public string campaign_name_en {get;set;}
     public int add_year {get;set;}
     public int add_month {get;set;}
     public double price {get;set;}
     public string begin_date {get;set;}
     public string end_date {get;set;}
     public string promotion_code {get;set;}
     public string promotion_public {get;set;}
     public int site_id {get;set;}

     public tcdc_mst_campaign() {
          this.DB = new DBUtil();
          DB.strConn = strConn;
     }

     public DataTable getById(string id) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_campaign" + 
                       " WHERE campaign_id = '" + id + "'";

          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public DataTable getByObject(tcdc_mst_campaign obj) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_campaign " + 
                       "WHERE 1 = 1 ";

          if(obj.campaign_id != null && 
             obj.campaign_id.ToString() != null && 
             obj.campaign_id.ToString() != "0")
          {
               sql += "AND campaign_id LIKE '%" + obj.campaign_id + "%' ";
          }
          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               sql += "AND product_id LIKE '%" + obj.product_id + "%' ";
          }
          if(obj.member_type_id != null && 
             obj.member_type_id.ToString() != null && 
             obj.member_type_id.ToString() != "0")
          {
               sql += "AND member_type_id LIKE '%" + obj.member_type_id + "%' ";
          }
          if(obj.campaign_group_id != null && 
             obj.campaign_group_id.ToString() != null && 
             obj.campaign_group_id.ToString() != "0")
          {
               sql += "AND campaign_group_id LIKE '%" + obj.campaign_group_id + "%' ";
          }
          if(obj.campaign_name_th != null && 
             obj.campaign_name_th.ToString() != null && 
             obj.campaign_name_th.ToString() != "0")
          {
               sql += "AND campaign_name_th LIKE '%" + obj.campaign_name_th + "%' ";
          }
          if(obj.campaign_name_en != null && 
             obj.campaign_name_en.ToString() != null && 
             obj.campaign_name_en.ToString() != "0")
          {
               sql += "AND campaign_name_en LIKE '%" + obj.campaign_name_en + "%' ";
          }
          if(obj.add_year != null && 
             obj.add_year.ToString() != null && 
             obj.add_year.ToString() != "0")
          {
               sql += "AND add_year LIKE '%" + obj.add_year + "%' ";
          }
          if(obj.add_month != null && 
             obj.add_month.ToString() != null && 
             obj.add_month.ToString() != "0")
          {
               sql += "AND add_month LIKE '%" + obj.add_month + "%' ";
          }
          if(obj.price != null && 
             obj.price.ToString() != null && 
             obj.price.ToString() != "0")
          {
               sql += "AND price LIKE '%" + obj.price + "%' ";
          }
          if(obj.begin_date != null && 
             obj.begin_date.ToString() != null && 
             obj.begin_date.ToString() != "0")
          {
               sql += "AND begin_date LIKE '%" + obj.begin_date + "%' ";
          }
          if(obj.end_date != null && 
             obj.end_date.ToString() != null && 
             obj.end_date.ToString() != "0")
          {
               sql += "AND end_date LIKE '%" + obj.end_date + "%' ";
          }
          if(obj.promotion_code != null && 
             obj.promotion_code.ToString() != null && 
             obj.promotion_code.ToString() != "0")
          {
               sql += "AND promotion_code LIKE '%" + obj.promotion_code + "%' ";
          }
          if(obj.promotion_public != null && 
             obj.promotion_public.ToString() != null && 
             obj.promotion_public.ToString() != "0")
          {
               sql += "AND promotion_public LIKE '%" + obj.promotion_public + "%' ";
          }
          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               sql += "AND site_id LIKE '%" + obj.site_id + "%' ";
          }
          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public int insertData(tcdc_mst_campaign obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[14];

          prm[0] = new SqlParameter("@product_id", SqlDbType.Int);
          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               prm[0].Value = obj.product_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }

          prm[1] = new SqlParameter("@member_type_id", SqlDbType.Int);
          if(obj.member_type_id != null && 
             obj.member_type_id.ToString() != null && 
             obj.member_type_id.ToString() != "0")
          {
               prm[1].Value = obj.member_type_id;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }

          prm[2] = new SqlParameter("@campaign_group_id", SqlDbType.Int);
          if(obj.campaign_group_id != null && 
             obj.campaign_group_id.ToString() != null && 
             obj.campaign_group_id.ToString() != "0")
          {
               prm[2].Value = obj.campaign_group_id;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }

          prm[3] = new SqlParameter("@campaign_name_th", SqlDbType.NVarChar,250);
          if(obj.campaign_name_th != null && 
             obj.campaign_name_th.ToString() != null && 
             obj.campaign_name_th.ToString() != "0")
          {
               prm[3].Value = obj.campaign_name_th;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }

          prm[4] = new SqlParameter("@campaign_name_en", SqlDbType.NVarChar,250);
          if(obj.campaign_name_en != null && 
             obj.campaign_name_en.ToString() != null && 
             obj.campaign_name_en.ToString() != "0")
          {
               prm[4].Value = obj.campaign_name_en;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }

          prm[5] = new SqlParameter("@add_year", SqlDbType.Int);
          if(obj.add_year != null && 
             obj.add_year.ToString() != null && 
             obj.add_year.ToString() != "0")
          {
               prm[5].Value = obj.add_year;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }

          prm[6] = new SqlParameter("@add_month", SqlDbType.Int);
          if(obj.add_month != null && 
             obj.add_month.ToString() != null && 
             obj.add_month.ToString() != "0")
          {
               prm[6].Value = obj.add_month;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }

          prm[7] = new SqlParameter("@price", SqlDbType.Decimal);
          prm[7].Precision = 18;
          prm[7].Scale = 10;
          if(obj.price != null && 
             obj.price.ToString() != null && 
             obj.price.ToString() != "0")
          {
               prm[7].Value = obj.price;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }

          prm[8] = new SqlParameter("@begin_date", SqlDbType.DateTime);
          if(obj.begin_date != null && 
             obj.begin_date.ToString() != null && 
             obj.begin_date.ToString() != "0")
          {
               prm[8].Value = obj.begin_date;
          }
          else
          {
               prm[8].Value = DBNull.Value;
          }

          prm[9] = new SqlParameter("@end_date", SqlDbType.DateTime);
          if(obj.end_date != null && 
             obj.end_date.ToString() != null && 
             obj.end_date.ToString() != "0")
          {
               prm[9].Value = obj.end_date;
          }
          else
          {
               prm[9].Value = DBNull.Value;
          }

          prm[10] = new SqlParameter("@promotion_code", SqlDbType.NVarChar,50);
          if(obj.promotion_code != null && 
             obj.promotion_code.ToString() != null && 
             obj.promotion_code.ToString() != "0")
          {
               prm[10].Value = obj.promotion_code;
          }
          else
          {
               prm[10].Value = DBNull.Value;
          }

          prm[11] = new SqlParameter("@promotion_public", SqlDbType.NVarChar,50);
          if(obj.promotion_public != null && 
             obj.promotion_public.ToString() != null && 
             obj.promotion_public.ToString() != "0")
          {
               prm[11].Value = obj.promotion_public;
          }
          else
          {
               prm[11].Value = DBNull.Value;
          }

          prm[12] = new SqlParameter("@site_id", SqlDbType.Int);
          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               prm[12].Value = obj.site_id;
          }
          else
          {
               prm[12].Value = DBNull.Value;
          }

          prm[13] = new SqlParameter("@ReturnValue", SqlDbType.Int);
          prm[13].Direction = ParameterDirection.Output;

          DB.ExecuteNonQueryProc("sp_tcdc_mst_campaign_insert", prm, false);
          return (Int32)prm[13].Value;
     }

     public int updateData(tcdc_mst_campaign obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[14];

          prm[0] = new SqlParameter("@campaign_id", SqlDbType.Int);
          if(obj.campaign_id != null && 
             obj.campaign_id.ToString() != null && 
             obj.campaign_id.ToString() != "0")
          {
               prm[0].Value = obj.campaign_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }
          prm[1] = new SqlParameter("@product_id", SqlDbType.Int);
          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               prm[1].Value = obj.product_id;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }
          prm[2] = new SqlParameter("@member_type_id", SqlDbType.Int);
          if(obj.member_type_id != null && 
             obj.member_type_id.ToString() != null && 
             obj.member_type_id.ToString() != "0")
          {
               prm[2].Value = obj.member_type_id;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }
          prm[3] = new SqlParameter("@campaign_group_id", SqlDbType.Int);
          if(obj.campaign_group_id != null && 
             obj.campaign_group_id.ToString() != null && 
             obj.campaign_group_id.ToString() != "0")
          {
               prm[3].Value = obj.campaign_group_id;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }
          prm[4] = new SqlParameter("@campaign_name_th", SqlDbType.NVarChar,250);
          if(obj.campaign_name_th != null && 
             obj.campaign_name_th.ToString() != null && 
             obj.campaign_name_th.ToString() != "0")
          {
               prm[4].Value = obj.campaign_name_th;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }
          prm[5] = new SqlParameter("@campaign_name_en", SqlDbType.NVarChar,250);
          if(obj.campaign_name_en != null && 
             obj.campaign_name_en.ToString() != null && 
             obj.campaign_name_en.ToString() != "0")
          {
               prm[5].Value = obj.campaign_name_en;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }
          prm[6] = new SqlParameter("@add_year", SqlDbType.Int);
          if(obj.add_year != null && 
             obj.add_year.ToString() != null && 
             obj.add_year.ToString() != "0")
          {
               prm[6].Value = obj.add_year;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }
          prm[7] = new SqlParameter("@add_month", SqlDbType.Int);
          if(obj.add_month != null && 
             obj.add_month.ToString() != null && 
             obj.add_month.ToString() != "0")
          {
               prm[7].Value = obj.add_month;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }
          prm[8] = new SqlParameter("@price", SqlDbType.Decimal);
          prm[8].Precision = 18;
          prm[8].Scale = 10;
          if(obj.price != null && 
             obj.price.ToString() != null && 
             obj.price.ToString() != "0")
          {
               prm[8].Value = obj.price;
          }
          else
          {
               prm[8].Value = DBNull.Value;
          }
          prm[9] = new SqlParameter("@begin_date", SqlDbType.DateTime);
          if(obj.begin_date != null && 
             obj.begin_date.ToString() != null && 
             obj.begin_date.ToString() != "0")
          {
               prm[9].Value = obj.begin_date;
          }
          else
          {
               prm[9].Value = DBNull.Value;
          }
          prm[10] = new SqlParameter("@end_date", SqlDbType.DateTime);
          if(obj.end_date != null && 
             obj.end_date.ToString() != null && 
             obj.end_date.ToString() != "0")
          {
               prm[10].Value = obj.end_date;
          }
          else
          {
               prm[10].Value = DBNull.Value;
          }
          prm[11] = new SqlParameter("@promotion_code", SqlDbType.NVarChar,50);
          if(obj.promotion_code != null && 
             obj.promotion_code.ToString() != null && 
             obj.promotion_code.ToString() != "0")
          {
               prm[11].Value = obj.promotion_code;
          }
          else
          {
               prm[11].Value = DBNull.Value;
          }
          prm[12] = new SqlParameter("@promotion_public", SqlDbType.NVarChar,50);
          if(obj.promotion_public != null && 
             obj.promotion_public.ToString() != null && 
             obj.promotion_public.ToString() != "0")
          {
               prm[12].Value = obj.promotion_public;
          }
          else
          {
               prm[12].Value = DBNull.Value;
          }
          prm[13] = new SqlParameter("@site_id", SqlDbType.Int);
          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               prm[13].Value = obj.site_id;
          }
          else
          {
               prm[13].Value = DBNull.Value;
          }

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_campaign_update", prm, false);
     }

     public int deleteData(string id) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[1];

          prm[0] = new SqlParameter("@campaign_id", id);

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_campaign_delete", prm, false);
     }
}

}

